package com.bw.launcher.gateway

import android.annotation.SuppressLint
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.bw.common.mvi.BaseFragment
import com.bw.launcher.R
import com.bw.launcher.databinding.FragmentGatewayBinding
import com.bw.quickadapter.BaseQuickMultipleAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class GatewayFragment:BaseFragment<FragmentGatewayBinding,GatewayViewModel>() {

    private val adapter : GatewayAdapter by lazy { GatewayAdapter() }
    override fun createVM(): GatewayViewModel {
        return viewModels<GatewayViewModel> ().value
    }
    override fun createViewBinding(): ViewBinding {
        return FragmentGatewayBinding.inflate(inflate)
    }
    override fun initView() {
        v.gatewayDateTv.text = "12"
        val manager = GridLayoutManager(context,3)
        manager.orientation = GridLayoutManager.VERTICAL
        v.gatewayRv.layoutManager = manager
        adapter.setOnItemCLickListener { adapter, position ->
            Log.e("ZXY","桌面点击:$position")
            when(position){
                1->{ARouter.getInstance().build("/apps/NetAppsActivity").navigation()}
                2->{ARouter.getInstance().build("/apps/LocalAppsActivity").navigation()}
                3->{ARouter.getInstance().build("/attendance/signactivity").navigation()}
                5->{ARouter.getInstance().build("/journal/JournalActivity").navigation()}
                7->{ARouter.getInstance().build("/documents/DocumentsActivity").navigation()}
                9->{
                    //跳转到联系人
                    val intent = Intent()
                    intent.action ="android.intent.action.VIEW"
                    intent.addCategory("android.intent.category.DEFAULT")
                    intent.setClassName("com.bw.im","com.bw.im.MainActivity")
                    startActivity(intent)
                }
                10->{
                    //跳转到群组
                    val intent = Intent()
                    intent.action ="android.intent.action.VIEW"
                    intent.addCategory("android.intent.category.DEFAULT")
                    intent.setClassName("com.bw.im","com.bw.im.MainActivity")
                    startActivity(intent)
                }
                11->{
                    //跳转到组织架构
                    val intent = Intent()
                    intent.action ="android.intent.action.VIEW"
                    intent.addCategory("android.intent.category.DEFAULT")
                    intent.setClassName("com.bw.im","com.bw.im.MainActivity")
                    startActivity(intent)
                }
            }
        }
        val foot1View = LayoutInflater.from(context).inflate(R.layout.layout_official,null,false)
        val foot2View = LayoutInflater.from(context).inflate(R.layout.layout_knowledge,null,false)
        val lp1 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,ConvertUtils.dp2px(300f))
        val lp2 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,ConvertUtils.dp2px(300f))
        foot1View.layoutParams = lp1
        foot2View.layoutParams = lp2
        adapter.setFoodView(foot1View)
        adapter.setFoodView(foot2View)
        v.gatewayRv.adapter = adapter
        v.gatewaySmart.setOnRefreshListener{refreshLayout-> refreshLayout.finishRefresh()}
        v.gatewaySmart.setOnLoadMoreListener{refreshLayout -> refreshLayout.finishLoadMore()}
    }
    override fun initData() {
        vm.createData()
    }
    @SuppressLint("UnsafeRepeatOnLifecycleDetector")
    override fun bindState() {
        lifecycleScope.launch(Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    adapter.setNewInstance(it.tabs)
                }
            }
        }
    }
}